package com.bw.android.bwgao6week2a.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bw.android.bwgao6week2a.R;
import com.bw.android.bwgao6week2a.databinding.LiveItemBinding;
import com.bw.android.network.bean.LiveListBean;
import com.bw.ch.baselib.BaseBindingViewHolder;

import java.util.List;

/**
 * Author: conghao
 * Date: 2023/9/18 11:42
 * Description: LiveAdapter
 */
public class LiveAdapter extends RecyclerView.Adapter<BaseBindingViewHolder> {
    private final List<LiveListBean.ResultBean> mData;
    /**
     * 开启下拉刷新功能后，可能导致【holder.getAdapterPosition()】得到的位置发生偏移，
     * 需要通过【positionOffset】校正下标位置。
     */
    private final int positionOffset;
    public LiveAdapter(List<LiveListBean.ResultBean> data, int positionOffset) {
        this.mData = data;
        this.positionOffset = positionOffset;
    }
    @NonNull
    @Override
    public BaseBindingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding binding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),
                R.layout.live_item,
                parent,
                false
        );
        return new BaseBindingViewHolder(binding.getRoot());
    }

    @Override
    public void onBindViewHolder(@NonNull BaseBindingViewHolder holder, int position) {
        LiveItemBinding binding = DataBindingUtil.getBinding(holder.itemView);
        final LiveListBean.ResultBean liveBean = mData.get(position);
        binding.setLiveItem(liveBean);
        binding.liveStatus.setText(liveBean.getLiveStatus() == 1 ? "直播" : "录播");
        if (this.onItemClickListener != null) {
            binding.getRoot().setOnClickListener(v -> {
                onItemClickListener.onItemClick(holder.getAdapterPosition() - positionOffset);
            });
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    /**
     * 设置列表中item的点击事件
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public interface OnItemClickListener {
        public void onItemClick(int position);
    }
    public OnItemClickListener onItemClickListener;


}
